package com.wf.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.wf.domain.RepairInfo;
import com.wf.mapper.RepairInfoMapper;
import com.wf.service.RepairInfoService;
import com.wf.utils.AliOssUtil;
import com.wf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @author chemin
 * @since 2024/9/2
 */
@RestController
@RequestMapping("/repair")
public class RepairInfoController {
//    private RepairRequestsService repairRequestsService;

    @Autowired
    RepairInfoService repairInfoService;
    @Autowired
    private RepairInfoMapper repairInfoMapper;
    @Autowired
    AliOssUtil aliOssUtil;

    @PostMapping("/add")
    public Result add(@RequestBody RepairInfo repairInfo){
        boolean flag = repairInfoService.save(repairInfo);
        if(flag){
            return new Result(0,"添加成功",flag);
        }
        return new Result(1,"添加失败",flag);
    }


    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id") Integer id) {
        try {
            RepairInfo repairInfo = repairInfoService.getById(id);
            if (ObjectUtil.isNotEmpty(repairInfo)) {
                return new Result(0, "", repairInfo);
            }
            return new Result(1, "Not Found", repairInfo);
        } catch (Exception e) {
            // 可以记录异常信息
            e.printStackTrace();
            return new Result(1, "An error occurred", null);
        }
    }


    @PutMapping("/update")
    public Result update( @RequestBody RepairInfo repairRequests){
        boolean flag = repairInfoService.updateById(repairRequests);
        if(flag){
            return new Result(0,"更新成功",flag);
        }
        return new Result(0,"更新成功",flag);
    }


    @PutMapping("/deleteRepair")
    public Result deleteRepair(Integer id){
        boolean flag = false;
        int i = repairInfoMapper.deleteById(id);
        if (i>0){
            flag=true;
        }
        return Result.success(flag);
    }


    @GetMapping("/findAll")
    public Result findAll(){
        List<RepairInfo> repairRequestsList = repairInfoService.list();
        if(CollectionUtil.isNotEmpty(repairRequestsList)){
            return new Result(0,"成功",repairRequestsList);
        }
        return new Result(1,"失败",repairRequestsList);
    }
}
